$(document).ready(function(){
	$('.redirect-auto-calendar-month').on('change',function(){
		var url = $(this).attr('data-url');
		var val = $(this).val();
		var add = $(this).attr('data-add');
		var sec = $(this).attr('data-section');

	var doit = function(url,segment){
		window.location.href = url+"/"+add+"/"+val+"/"+sec;
	};

	$("#msgDialog").dialog('option','height',200);
	$( '#msgDialog > p' ).html( 'Please wait while system generates bills.' );
	$('.ui-dialog-buttonpane').find('button').remove();
	$('.ui-dialog-titlebar').find('button').remove();
	$( '#msgDialog > div.content' ).text('Please Wait.');
	$(".ui-dialog-titlebar-close").hide();
	 $("#msgDialog").dialog('option','buttons','');
	$( '#msgDialog > p' ).text( 'Redirecting' );
	$("#msgDialog").dialog("open");
	
	setInterval(doit(url,val),1000);
	});

	$('.redirect-auto-calendar-year').on('change',function(){
		var url = $(this).attr('data-url');
		var val = $(this).val();
		var add = $(this).attr('data-add');
		var sec = $(this).attr('data-section');

	var doit = function(url,segment){
		window.location.href = url+"/"+val+"/"+add+"/"+sec;
	};

	$("#msgDialog").dialog('option','height',200);
	$( '#msgDialog > p' ).html( 'Please wait while system generates Bills.' );
	$('.ui-dialog-buttonpane').find('button').remove();
	$('.ui-dialog-titlebar').find('button').remove();
	$( '#msgDialog > div.content' ).text('Please Wait.');
	$(".ui-dialog-titlebar-close").hide();
	 $("#msgDialog").dialog('option','buttons','');
	$( '#msgDialog > p' ).text( 'Redirecting' );
	$("#msgDialog").dialog("open");
	
	setInterval(doit(url,val),1000);
	});



    $('.show-event').click(function(e) {
    	e.preventDefault();    
    	var url = $(this).attr('url');
    	var section_name = $(this).text();
		$.blockUI({ 
			css: {
				border: 'none', 
				padding: '15px', 
				backgroundColor: '#000', 
				'-webkit-border-radius': '10px', 
				'-moz-border-radius': '10px', 
				opacity: .5, 
				color: '#fff' 
			},
			message: 'Fetching Calendar Events'
		});
        	
		$.ajax({ 
            url: url,
            type:"POST",
            cache: false
         }).done(function(output){
         	$.blockUI({
				theme:true,
				title:' ',
				message: output
			});

         	$('div.blockUI.blockMsg.ui-dialog').css('width','70%')
         									   .css('height','90%')
         									   .css('left','15%')
         									   .css('right','15%')
         									   .css('top','2%');
        	$('div.ui-widget-header.blockTitle').append().html('<button class="btn btn-mini btn-danger" id="close">Close</button> Details on event: '+ section_name);
        	$('div.ui-widget-content.ui-dialog-content').css('max-height','90%');
			$('.blockOverlay').attr('title','Click anywhere to close.').click($.unblockUI);
			$('#close').click($.unblockUI);
         }).fail(function(){
         	$.unblockUI();
         	$( '#msgDialog > p' ).text( 'ERROR!' );
			$('.ui-dialog-buttonpane').find('button').remove();
			$( '#msgDialog > div.content' ).text('Sorry but system were unable to fetch details from this event.');
			$("#msgDialog").dialog("open");
         }); 
    }); 


	$(document).on('click','a.delete-event',function(e){
		e.preventDefault();
		
		var batchid = $(this).attr('data-batchid');
		var url = $(this).attr('href');
     	$.blockUI({
			theme:true,
			title:' ',
			message: 'Are you sure you want to delete this event?'
		});

     	$('div.blockUI.blockMsg.ui-dialog').css('width','50%')
     									   .css('height','40%')
     									   .css('left','25%')
     									   .css('right','25%')
     									   .css('top','2%');
    	$('div.ui-widget-header.blockTitle').append().html('<button class="btn btn-success" id="close">Close Dialog</button>Delete Event?');
    	$('div.ui-widget-content.ui-dialog-content').css('max-height','90%');
    	$('div.ui-widget-content.ui-dialog-content').html('<p>Are you sure you want to delete event? Changes cannot be reverted.</p><button id="delete" class="btn btn-danger">Delete Event</button>');
    	// $('div.ui-widget-content.ui-dialog-content').html('<button id="delete">Delete File</button>');
		$('.blockOverlay').attr('title','Click anywhere to close').click($.unblockUI);
		$('#close').click($.unblockUI);
		$(document).on('click','button#delete',function(e){
			e.preventDefault();
			$.ajax({ 
	            url: url,
	            type:"POST",
	            cache: false
	         }).done(function(output){
	         	if(output ==1)
	         	{
	         		//hide events
	         		$('a.'+batchid).hide('slow');

	         		//show dialog
	         		$.blockUI({ 
	                    message: 'Event was successfully deleted.', 
	                    fadeIn: 700, 
	                    fadeOut: 2000, 
	                    timeout: 4000, 
	                    showOverlay: false, 
	                    centerY: false, 
	                    css: { 
	                        width: '350px', 
	                        top: '60px', 
	                        left: '', 
	                        right: '10px', 
	                        border: 'none', 
	                        padding: '5px', 
	                        backgroundColor: '#000', 
	                        '-webkit-border-radius': '10px', 
	                        '-moz-border-radius': '10px', 
	                        opacity: .6, 
	                        color: '#fff' 
	                    } 
	               	 }); 
	         	}else{
	         		 $.blockUI({ 
	                    message: 'Unable to delete event. Error occured.', 
	                    fadeIn: 700, 
	                    fadeOut: 2000, 
	                    timeout: 4000, 
	                    showOverlay: false, 
	                    centerY: false, 
	                    css: { 
	                        width: '350px', 
	                        top: '60px', 
	                        left: '', 
	                        right: '10px', 
	                        border: 'none', 
	                        padding: '5px', 
	                        backgroundColor: '#000', 
	                        '-webkit-border-radius': '10px', 
	                        '-moz-border-radius': '10px', 
	                        opacity: .6, 
	                        color: '#fff' 
	                    } 
	               	 }); 
	         	}
			}).fail(function(){
	     		 $.blockUI({ 
	                message: 'Unable to delete event. Error occured.', 
	                fadeIn: 700, 
	                fadeOut: 2000, 
	                timeout: 4000, 
	                showOverlay: false, 
	                centerY: false, 
	                css: { 
	                    width: '350px', 
	                    top: '60px', 
	                    left: '', 
	                    right: '10px', 
	                    border: 'none', 
	                    padding: '5px', 
	                    backgroundColor: '#000', 
	                    '-webkit-border-radius': '10px', 
	                    '-moz-border-radius': '10px', 
	                    opacity: .6, 
	                    color: '#fff' 
	                } 
	           	 }); 
			});
		});

	});
});



